﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using Peter.Common;
using Peter.Common.MainMenu;

namespace Peter.Logic
{
   /// <summary>
   /// Model for the main menu in Peter.
   /// </summary>
   public class ModelMenu : ViewModelBase, IPartImportsSatisfiedNotification
   {
      /// <summary>
      /// Initializes a new menu model.
      /// </summary>
      public ModelMenu ()
      {
         this.Items = new ObservableCollection<IMainMenuHeader> ();
         this.QuickAccessItems = new ObservableCollection<IMainMenuQuickAccessItem> ();
      }

      /// <summary>
      /// Gets the collection of children of the menu item.
      /// </summary>
      public ObservableCollection<IMainMenuHeader> Items { get; private set; }

      /// <summary>
      /// Gets or Sets the quick access menu items.
      /// </summary>
      public ObservableCollection<IMainMenuQuickAccessItem> QuickAccessItems { get; private set; }

      /// <summary>
      /// Gets the list of menu items.
      /// </summary>
      [ImportMany (typeof (IMainMenuItem))]
      public IEnumerable<IMainMenuItem> MenuItems { get; set; }

      /// <summary>
      /// Gets the list of menu items.
      /// </summary>
      [ImportMany (typeof (IMainMenuHeader))]
      public IEnumerable<IMainMenuHeader> HeaderItems { get; set; }

      /// <summary>
      /// Gets the list of menu items.
      /// </summary>
      [ImportMany (typeof (IMainMenuQuickAccessItem))]
      public IEnumerable<IMainMenuQuickAccessItem> QaItems { get; set; }

      /// <summary>
      /// Called when a part's imports have been satisfied and it is safe to use.
      /// </summary>
      public void OnImportsSatisfied ()
      {
         // Add headers...
         foreach (var header in this.HeaderItems.OrderBy (h => h.Priority))
         {
            this.GuiDispatcher.Invoke (new Action<IMainMenuHeader> (h => this.Items.Add (h)), header);
         }

         // Add menu items...
         int a = 99;
         foreach (var item in this.MenuItems.OrderBy (m => m.HeaderName).ThenBy (m => m.Priority))
         {
            var addItemTo = this.Items [0];
            if (!string.IsNullOrEmpty (item.HeaderName))
            {
               var addItem = this.Items.FirstOrDefault (h => h.Title.ToUpper () == item.HeaderName.ToUpper ());
               if (addItem != null)
                  addItemTo = addItem;
            }

            if (item.Priority > a)
            {
               this.GuiDispatcher.Invoke (() => addItemTo.Children.Add (new MainMenuSeperator ()));
               a += 100;
            }

            this.GuiDispatcher.Invoke (
               item.IsHeaderMenuItem
                  ? new Action <IMainMenuItem> (i => addItemTo.MenuItems.Add (i))
                  : (i => addItemTo.Children.Add (i)), item);
         }

         // Add Quick access...
         foreach (var item in this.QaItems.OrderBy (m => m.Priority))
         {
            this.GuiDispatcher.Invoke (new Action<IMainMenuQuickAccessItem> (i => this.QuickAccessItems.Add (i)), item);
         }
      }
   }
}
